package day11;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserTest {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎来到学生管理系统！！！");
        while(true) {
            System.out.println("请选择操作：1--登录 2--注册 3--忘记密码 ");
            int n = sc.nextInt();
            switch (n) {
                case 1 -> register(list);
                case 2 -> login(list);
                case 3 -> forgetPassword(list);
                default -> System.out.println("输入错误，请重新输入：");
            }
        }
    }
    public static void register(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
       while(true){
           System.out.println("请输入用户名：");
           String name = sc.next();
           if(NI(list,name)==-1) {
               System.out.println("用户名输入错误！");
               continue;
           }
           while (true){
               System.out.println("请输入密码：");
               String mima =sc.next();
               String creat =creat();
               System.out.println("验证码为："+creat+"\n请输入验证码：");
               String yanzhengma =sc.next();
               if(mima.equals(list.get(NI(list,name)).password) && creat.equals(yanzhengma)) {
                   System.out.println("登录成功！");
                   return;
               }else{
                   System.out.println("密码或验证码输入错误，请重新输入：");
               }
           }
       }

    }
    public static String creat(){
        String creat ="";
        Random r = new Random();
        ArrayList<Character> l =new ArrayList<>();
        for(int i=0 ;i<26 ;i++){
            l.add((char) ('a'+i));
        }
        for(int i=0 ;i<26 ;i++){
            l.add((char) ('A'+i));
        }
        for(int i=0 ;i<10 ;i++){
            l.add((char) ('0'+i));
        }
        for (int i=0;i<5;i++){
            int k=r.nextInt(62)+1;
            creat+=l.get(k);
        }
        return creat;
    }
    public static int NI (ArrayList<User> list ,String name){
        for (int i = 0; i < list.size(); i++) {
            String n=list.get(i).userName;
            if(name.equals(n)) return i;
        }
        return -1;
    }
    public static void login (ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        User u = new User();
        System.out.println("请输入用户名：");
        while(true){
            String name =sc.next();
            Boolean flag = checkname(list,name);
            if(flag) {
                u.setUserName(name);
                break;
            }
            else{
                System.out.println("请重新输入！");
            }
        }
        System.out.println("请输入密码");
        while(true){
            String password =sc.next();
            Boolean flag = checkPassword(password);
            if(flag) {
                u.setPassword(password);
                break;
            }
            else{
                System.out.println("两次输入不同" );
                System.out.println("请重新输入！");
            }
        }
        System.out.println("请输入身份证号：");
        while(true){
            String id =sc.next();
            Boolean flag = checkid(id);
            if(flag) {
                u.setId(id);
                break;
            }
            else{
                System.out.println("请输入正确的身份证号码\n请重新输入！");
            }
        }        System.out.println("请输入手机号码：");
        while(true){
            String pn =sc.next();
            Boolean flag = checkpn(pn);
            if(flag) {
                u.setPhoneNumber(pn);
                break;
            }
            else{
                System.out.println("请输入正确的手机号码\n请重新输入！");
            }
        }
        System.out.println("注册成功！");
        list.add(u);
    }
    public static Boolean checkpn(String pn){
        if(pn.length() !=11 || pn.charAt(0) == '0' ||CP1(pn)) return  false;
        else return true;
    }
    public static Boolean CP1(String pn){
        for(int i=0;i<pn.length();i++){
            if(pn.charAt(i)<'0' || pn.charAt(i)>'9') return true;
        }
        return false;
    }
    public static Boolean checkid(String id){
        if(id.length()!=18 || id.charAt(0)=='0' || CI1(id)) return false;
        else return true;
    }
    public static Boolean CI1(String id){
        for(int i=0;i<id.length();i++){
            if(i==id.length()-1) {
                if(id.charAt(i)<'0' || id.charAt(i)>'9'){
                    if(id.charAt(i)!='x' && id.charAt(i)!='X') return true;
                }
            }
            else {
                if(id.charAt(i)<'0' || id.charAt(i)>'9') return true;
            }
        }
        return false;
    }

    public static Boolean checkPassword(String password){
        Scanner sc =new Scanner(System.in);
        System.out.println("请再次输入密码：");
        String p1=sc.next();
        if(p1.equals(password)) return true;
        else return false;
    }
    public static Boolean checkname(ArrayList<User> list,String name){
        //长度必须在3-15之间
        if(name.length()<3 || name.length()>15) {
            System.out.println("用户名长度必须在3-15之间！");
            return false;
        }
        //用户名唯一
        else if(CN1(list,name)) {
            System.out.println("用户名重复！");
            return false;
        }
        //必须是数字加字母不是纯数字
        else if(CN2(name)) {
            System.out.println("用户名必须是数字加字母！");
            return false;
        }
        else  return true;

    }
    public static Boolean CN1(ArrayList<User> list,String name){
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i).userName;
            if(s.equals(name)) {
                return true;
            }
        }
        return false;
    }
    public static Boolean CN2(String name){
        int count1=0,count2=0;
        for (int i = 0; i < name.length(); i++) {
            if(name.charAt(i)<='z' && name.charAt(i)>='a') count1++;
            if(name.charAt(i)<='Z' && name.charAt(i)>='A') count1++;
            if(name.charAt(i)<='9' && name.charAt(i)>='0') count2++;
        }
        if(count1==0 || count2==0) return true;
        else return false;
    }
    public static void forgetPassword(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入用户名：");
            String name = sc.next();
            if(NI(list,name)==-1) {
                System.out.println("用户名输入错误！\n请重新输入");
                continue;
            }
            while(true){
                System.out.println("请输入绑定身份证号码：");
                String id =sc.next();
                System.out.println("请输入绑定手机号码：");
                String phone =sc.next();
                if(id.equals(list.get(NI(list,name)).id) && phone.equals(list.get(NI(list,name)).phoneNumber)){
                    System.out.println("验证成功，请重置密码：");
                    while(true){
                        String password =sc.next();
                        Boolean flag = checkPassword(password);
                        if(flag) {
                            list.get(NI(list,name)).setPassword(password);
                            System.out.println("密码重置成功！");
                            return;
                        }
                        else{
                            System.out.println("两次输入不同" );
                            System.out.println("请重新输入！");
                        }
                    }
                }
                else System.out.println("身份证号码或手机号码输入错误，请重新输入：");
            }
        }
    }
}
